// 权限按钮展示指令
import {crmStore} from '@CRM/store'

export default {
  install: (app) => {
    app.directive('auth', {
      mounted(el, binding, vnode) {
        const { v } = binding.value
        let bool = true
        if (binding.value.bool !== undefined && typeof (binding.value.bool) === 'boolean') {
          bool = binding.value.bool
        }
        const allPermission = '*:*:*'
        const permissions = crmStore.getters['user/permissions']
        if (v && v instanceof Array && v.length > 0) {
          const hasPermissions = permissions.some(permission => {
            return allPermission === permission || v.includes(permission)
          })
          if (!hasPermissions) {
            el.remove()
          } else {
            if (bool) {
              el.style.display = 'inline-block'
            } else {
              el.style.display = 'none'
            }
          }
        } else {
          console.log(`请设置操作权限标签值`)
        }
      },
      updated(el, binding, vnode, oldVnode) {
        let bool = true
        if (binding.value.bool !== undefined && typeof (binding.value.bool) === 'boolean') {
          bool = binding.value.bool
        }
        if (bool) {
          el.style.display = 'inline-block'
        } else {
          el.style.display = 'none'
        }
      }
    })
  }
}

